<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>登录</title>
    <link rel="stylesheet" href="../css/loginall.css">
    <link rel="stylesheet" href="http://at.alicdn.com/t/font_3414996_ncgzq158vn.css">
    
</head>
<body>
    <div class="loginwrap">
        <div class="login-con">
            <h1 class="logo" >SIGN IN</h1>
            <h1 class="title">登录</h1>
            <div class="username inputs">
                <input type="text" name="" id="username" placeholder="请输入用户名">
            </div>
            <div class="pasword inputs">
                <input type="password" name="" id="password" placeholder="请输入密码">
            </div>
            <button id="loginBtn">登录</button>
            <button id="regBtn">新用户注册</button>
            <div class="wachlog">
                <div class="wacbox">
                    <a href="./index.html" target="_self">
                       <i class="iconfont icon-shouye"></i> 返回首页
                    </a>
                </div>
            </div>
        </div>
    </div>

    <script src="../js/axios.min.js"></script>
    <script>
        
        // 点击注册跳转注册页
        var regbtn = document.querySelector("#regBtn");
        regbtn.onclick = function(){
            location.href = './register.html';
        }

        // 点击登录调接口,获取输入框用户名密码
        var loginBtn = document.querySelector("#loginBtn");
        // console.log(loginBtn);
        loginBtn.onclick = function(){
            var username = document.querySelector("#username").value;
            var password = document.querySelector("#password").value;
            if(username == ''||password==''){
                alert("请检查输入是否正确");
                return;
            }
            // console.log(username,password);
            let loginAPI = 'http://jx.xuzhixiang.top/ap/api/login.php';
            let p = axios.get(loginAPI,{params:{username,password}});
            p.then(r=>{
                console.log(r);
                if(r.data.code == 0){
                    alert(r.data.msg);
                }else{
                    // 登陆成功,跳转首页
                    // 用户名密码存入本地
                    var userArr = r.data.data;
                    // console.log(userArr);
                    localStorage.setItem("usname",userArr.username);
                    localStorage.setItem("uid",userArr.id);
                    alert(r.data.msg+",跳转首页");
                    location.href = './index.html';

                }
            })
        }
    </script>
</body>
</html>